/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package task20;
import java.io.File;
import java.io.IOException;
import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 *
 * @author Administrator
 */
public class AddressBooks {
    public AddressBooks(){}
    public boolean newCard(AddressBookBean newcard) {
        Document doc;
        Element addressbook,card;
        Element name,mobile,office,home,qq,email,other;
        boolean flag=false;
        try{
            DocumentBuilderFactory f=DocumentBuilderFactory .newInstance();
            DocumentBuilderFactory db=f.newDocumentBuilder*();
            File file=new File("addreebook");
            if(!file.exists()){
                file.mkdir();
            }
            file=new File("addreebook/addressbook.xml");
            if(!file.exists()){
                flag=true;
                file.createNewFile();
                doc=db.newDocument();
                addressbook=doc.createElement("addressbook");
            }else{
                doc=db.parse(file);
                addressbook=(Element)doc.getElementByTagName("addressbook").item(0);
            }
            card=doc.createElement("card");
            name=doc.createElement("name");
            mobile=doc.createElement("mobile");
            office=doc.createElement("office");
            home=doc.createElement("home");
            qq=doc.createElement("qq");
            email=doc.createElement("email");
            other=doc.createElement("other");
            
            
            name.appendChild(doc.createTextNode(newcard.getName()));
            mobile.appendChild(doc.createTextNode(newcard.getMobile()));
            office.appendChild(doc.createTextNode(newcard.getOffice()));
            home.appendChild(doc.createTextNode(newcard.getHome()));
            qq.appendChild(doc.createTextNode(newcard.getQQ()));
            email.appendChild(doc.createTextNode(newcard.getEmail()));
            other.appendChild(doc.createTextNode(newcard.getOther()));
            
            
            card.appendChild(name);
            card.appendChild(mobile);
            card.appendChild(office);
            card.appendChild(home);
            card.appendChild(qq);
            card.appendChild(email);
            card.appendChild(other);
            addressbook.appendChild(card);
            
            }
        }
    }
        
    

}
